<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
    <title>INSPINIA | Basic Form</title>
    <title>
        <c:if test="${method == 'add'}">添加管理员</c:if>
        <c:if test="${method == 'update'}">编辑管理员</c:if>
    </title>
</head>

<body>

<div id="wrapper">
    <div class="gray-bg">
        <div class="row m-t-sm">
            <div class="col-lg-12">
                <ol class="breadcrumb f-fl">
                    <li>
                        <i class="fa fa-location-arrow"></i>   <a>管理员</a>
                    </li>
                    <li class="active">
                        <strong>
                        <c:if test="${method == 'add'}">添加管理员</c:if>
                        <c:if test="${method == 'update'}">编辑管理员</c:if>
                        </strong>
                    </li>
                </ol>
                <span class="navbar-right  m-r m-t-8"><a href="javascript:history.go(-1);" class="btn-primary btn-add "> <i class='fa fa-mail-reply'></i> 返回</a></span>
            </div>
        </div>
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-title">
                            <h5>注意： <small>请填写真实用户信息，否则将影响您的使用.</small></h5>
                        </div>
                        <div class="ibox-content">
                            <form method="get" data-ajax="${ctx}/admin/sys/admin/list"   form-before="before()"   action="${ctx}/admin/sys/admin/${method}"  class="form-horizontal" id="user_form">
                                <input  type="hidden" name="adminId" id="adminId"  value="${admin.adminId}"/>
                                <div class="form-group"><label class="col-sm-2 control-label">账号</label>
                                    <div class="col-sm-6"><input type="text" name="username" id="username" placeholder="请输入账号" value="${admin.username}" class="form-control" required></div>
                                </div>
                                <c:if test="${method=='update'}">
                                <div class="form-group"><label class="col-sm-2 control-label">密码</label>
                                    <div class="col-sm-6"><button type="button" class="btn btn-w-m btn-default" onclick="resetPsd()">重置密码</button>  <label class="control-label" style="font-weight:100; padding-left: 10px;">重置后密码 "123456"</label></div>
                                </div>
                                </c:if>
                                <div class="form-group"><label class="col-sm-2 control-label">姓名</label>
                                    <div class="col-sm-6"><input type="text" class="form-control" placeholder="请输入姓名" value="${admin.name}" name="name" required></div>
                                </div>
                                <div class="form-group"><label class="col-sm-2 control-label">手机号</label>
                                    <div class="col-sm-6"><input type="text" class="form-control" isMobile placeholder="请输入手机号" value="${admin.tel}" name="tel" required></div>
                                </div>
                                
                                <c:if test="${adminType==1}">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">用户类型</label>
                                    <div class="col-sm-6">
                                        <label class="checkbox-inline i-checks"><input type="radio" value="1" id="adminType1" name="adminType" class="{required:true}" data-checked="${admin.adminType}">超级管理员</label>
                                        <label class="checkbox-inline i-checks"><input type="radio" value="2" id="adminType2" name="adminType">普通管理员</label>
                                    </div>
                                </div>
                                </c:if>

                                <div class="form-group"><label class="col-sm-2 control-label">角色</label>
                                    <div class="col-sm-6 select-role-box">
                                        <select class="select2_role form-control" name="roles" multiple="multiple" required>
                                            <c:forEach var="role" items="${roles}">
                                                <option value="${role.roleId}">${role.roleName}</option>
                                            </c:forEach>
                                        </select>

                                    </div>
                                </div>
                                <div class="form-group"><label class="col-sm-2 control-label">职位</label>
                                    <div class="col-sm-6"><input type="text" name="position" class="form-control" placeholder="请输入职位" value="${admin.position}" required></div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="submit">保 存</button>
                                        <a href="javascript:history.go(-1);" class="btn btn-white ">  取消</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script>

    $(document).ready(function() {
         var select2_role =  $(".select2_role").select2();

        <c:if test="${method=='update'}">
              var rids="${rids}";
              select2_role.val(eval("(["+rids+"])")).trigger('change');
        </c:if>
    });

    var before =function(){
        var username=$("#username").val();
        var adminId=$("#adminId").val();

        var relt;
        $.ajax({
            url: "${ctx}/admin/sys/admin/existed",
            method: "POST",
            async: false,
            data: {"username" : username,"adminId":adminId},
            dataType: "json",
            success : function(response) {
                if (response.success) {
                    relt= true;
                } else {
                    parent.layer.alert(username+'已存在');
                    relt = false;
                }
            }
        });
        return relt;
    }

    var resetPsd =function(){
        parent.layer.confirm('是否确认重置密码？', function() {
            $.ajax({
                url: "${ctx}/admin/sys/admin/resetPass",
                method: "POST",
                data: {"adminId" : "${admin.adminId}"},
                dataType: "json",
                success : function(response) {
                    if (response.success) {
                        parent.layer.alert('重置成功');
                    } else {
                        parent.layer.alert('重置失败');
                    }
                }
            });
        })
    }
</script>
</body>

</html>
